package com.nosweattechnet.placemap;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import com.nosweattechnet.datatypes.Item;
import com.nosweattechnet.mapmyway.R;
import com.nosweattechnet.server.ItemListAsyncTask;

import android.os.Bundle;
import android.app.Activity;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

//This activity is responsible for getting a user's favorite items
public class ItemListActivity extends Activity {

	private String userid;
	private String placeid;
	private LinearLayout ll;
	private ArrayList<Item> items;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent=getIntent();
        placeid=intent.getStringExtra(getString(R.string.place_id_intent));
        userid=intent.getStringExtra(getString(R.string.user_id_intent));
        ItemListAsyncTask ilat=new ItemListAsyncTask(userid,placeid);
        ilat.execute();
        LayoutInflater inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ScrollView parentView=(ScrollView) inflater.inflate(R.layout.activity_item_list,null);
        ll=(LinearLayout)parentView.getChildAt(0);
        items=new ArrayList<Item>();
        try {
			items=ilat.getItems();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (ExecutionException e) {
			e.printStackTrace();
		}
		
        //Checks for empty item list
        if(items.size()<=0){
        	Toast.makeText(this,R.string.no_Item_Favorites,Toast.LENGTH_LONG).show();
        	finish();
        }
        
        for(Item i:items){
        	View t=makeView(i);
        	ll.addView(t);
        }
        
        setContentView(parentView);
    }
    
	//Displays an item and its details
    private View makeView(final Item i){
    	RelativeLayout rl=new RelativeLayout(this);
    	
    	TextView ttitle=new TextView(this);
    	ttitle.setText(i.getName());
    	ttitle.setTextSize(20);
    	RelativeLayout.LayoutParams paramstitle=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    	paramstitle.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    	paramstitle.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    	rl.addView(ttitle,paramstitle);
    	ttitle.setId(1);
    	
    	TextView tsubtitle=new TextView(this);
    	tsubtitle.setText(i.getDescription());
    	tsubtitle.setTextSize(12);
    	RelativeLayout.LayoutParams paramssubtitle=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    	paramssubtitle.addRule(RelativeLayout.BELOW,ttitle.getId());
    	paramssubtitle.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    	rl.addView(tsubtitle,paramssubtitle);
    	rl.setOnClickListener(new OnClickListener(){
			//When a user clicks on an item, display item on map
			public void onClick(View v) {
				DisplayItems.addItem(i);
				finish();
			}
    		
    	});
    	return rl;
    }

	//Standard options menu creation
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_item_list, menu);
        return true;
    }

    //When item on standard options menu is selected
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        	case R.id.item_list_show_all:
        		DisplayItems.addItems(items);
        		finish();
        		return true;
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

}
